<?php

class __mail_mail extends Module {
	
	public function __construct () {
		parent::__construct ('mail');	
	}

	/**
	 * Оповещение пользователя о новом сообщении
	 * @param Object $oMessage объект сообщения
	 * @return void
	 */
	public function messageNotification (Object $oMessage) {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		$oUser = $objectsController->getObject ($typesController->getTypeByName ('user'), $oMessage->getValue ('addressed_user_id'));
		
		$oTypeSettings = $typesController->getTypeByName ('user_settings');
		$selUserSettings = new Selection;
		$selUserSettings->addType ($oTypeSettings);
		$selUserSettings->addFilter ('user_id', $oUser->getId ());
		$selUserSettings->addLimit (1);
		
		$res = $selUserSettings->getList ();
		if (count ($res)) {
			$oSettings = $objectsController->getObject ($oTypeSettings, $res[0][0]);
			if ($oSettings->getValue ('notification_message') == 0) {
				return false;
			}
		}
				
		$template = new SimpleTemplate ('mail/mailNewMessage.tpl');
		$template->setVar ('{addressedUserName}', $oUser->getValue ('name'));
		$template->setVar ('{addressedUserLogin}', $oUser->getValue ('login'));
		$template->setVar ('{messageId}', $oMessage->getId ());
		$template->setVar ('{messageType}', $oMessage->getTypeName ());
		$template->setVar ('{messageSubject}', $oMessage->getValue ('subject'));
		
		Mailer::sendEMail(new EMail ($oUser->getValue ('email'), $template->display (), lang('email_new_mess_notif_subject','mail')));
	}
	
	/**
	 * Оповещение автора сообщения о новом комментарии
	 * @param Object $oMessage объект сообщения
	 * @param Object $oComment объект нового комментария
	 * @return void
	 */
	public function messageReplyNotification (Object $oMessage, Object $oComment) {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		if ($oComment->getValue ('owner_id') == $oMessage->getValue ('sender_id')) {
			$addressedUId = $oMessage->getValue ('addressed_user_id');
		} elseif ($oComment->getValue ('owner_id') == $oMessage->getValue ('addressed_user_id')) {
			$addressedUId = $oMessage->getValue ('sender_id');
		}
		
		$oAddressedUser = $objectsController->getObject ($typesController->getTypeByName ('user'), $addressedUId);
		
		$oTypeSettings = $typesController->getTypeByName ('user_settings');
		$selUserSettings = new Selection;
		$selUserSettings->addType ($oTypeSettings);
		$selUserSettings->addFilter ('user_id', $oAddressedUser->getId ());
		$selUserSettings->addLimit (1);
		
		$res = $selUserSettings->getList ();
		if (count ($res)) {
			$oSettings = $objectsController->getObject ($oTypeSettings, $res[0][0]);
			if ($oSettings->getValue ('notification_message') == 0) {
				return false;
			}
		}
		
		$oCurrentUser = ExplaySession::getUser ();
		
		$template = new SimpleTemplate ('mail/mailMessageReply.tpl');
		$template->setVar ('{addressedUserName}', $oAddressedUser->getValue ('name'));
		$template->setVar ('{addressedUserLogin}', $oAddressedUser->getValue ('login'));
		$template->setVar ('{commentOwnerLogin}', $oCurrentUser->getValue ('login'));
		$template->setVar ('{commentOwnerName}', $oCurrentUser->getValue ('name'));
		$template->setVar ('{commentId}', $oComment->getId ());
		$template->setVar ('{commentText}', $oComment->getValue ('text'));
		$template->setVar ('{messageId}', $oMessage->getId ());
		$template->setVar ('{messageSubject}', $oMessage->getValue ('subject'));
		
		Mailer::sendEMail(new EMail ($oAddressedUser->getValue ('email'), $template->display (), lang('email_mess_reply_subject','mail')));
	}
	
}